package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

// 0.测试
	@Override
	public List<Product> selectAll() {
		return productMapper.selectAll();
	}
	
//后台
	// 1.页面展示 
	@Override
	public Result selectByPage(Integer page, Integer limit, Product product) {
		//总个数
		int count = productMapper.getTotalCount(product);
		
		//每页信息
		int offset = (page-1)*limit;
		List<Product> list = productMapper.selectProductByPage(offset,limit,product);
		
		return Result.success("", list, count);
	}

//2.批量删除
	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;				//1.记数
		for (Integer id : ids) {	//2.循环删除，根据id.length 有几个id删除几次
			int c = productMapper.deleteByPrimaryKey(id); //3. 每次 根据id删除
			count+=c;	//4.删除一次，删除记录+1
		}
		return count== ids.length ? true:false; // 5.删除记录次数 == id[]个数相等 (确保都删了)；删除成功，否则失败05
	}

//3.删除
	@Override
	public boolean deleteById(Integer id) {
		int count= productMapper.deleteByPrimaryKey(id);
		return count==1 ? true:false;
	}
//4.添加
	@Override
	public boolean insert(Product product) {
		int count = productMapper.insert(product);
		return count>0 ? true : false;
	}

//前台
	// by二级id 找list<Product>: 属于它的所有商品
	@Override
	public List<Product> selectProductListBySecondCategoryId(Integer secondCategoryId) {
		System.out.println("ProductServiceImpl.selectProductListBySecondCategoryId()");
		return productMapper.selectProductListBySecondCategoryId(secondCategoryId);
	}

	//2.商品详情 页面   拿到product商品详情
	@Override
	public Product selectProductByProductId(Integer productId) {
		System.out.println("ProductServiceImpl.selectProductByProductId()");
		
		return productMapper.selectProductByProductId(productId);
	}

	

}
